﻿using System;
using System.Collections.Generic;

namespace Ricebird.Wordprocessing
{
    /// <summary>
    /// 一切元素的基类
    /// </summary>
    public abstract class CompositeElementBase
    {
        #region ctor
        protected CompositeElementBase BaseElement
        {
            get;
            set;
        }

        protected WordDocument Document
        {
            get;
            set;
        }

        protected List<CompositeElementBase> Children
        {
            get; set;
        } = new List<CompositeElementBase>();

        public CompositeElementBase(CompositeElementBase @base)
        {
            if (@base == null)
            {
                throw new NullReferenceException("@base 不能为 null");
            }
            BaseElement = @base;
            Document = @base.Document;
        }

        public CompositeElementBase(WordDocument doc)
        {
            Document = doc;
            BaseElement = null;
        }

        /// <summary>
        /// 初始化Properties
        /// </summary>
        public abstract void InitializeElement();
        #endregion

        #region 流式接口
        /// <summary>
        /// 添加一个空段落
        /// </summary>
        /// <returns></returns>
        public abstract CompositeElementBase AppendParagraph();
        /// <summary>
        /// 添加一个空段落
        /// </summary>
        /// <param name="style">适用样式集</param>
        /// <returns></returns>
        public abstract CompositeElementBase AppendParagraph(WordStyles style);
        /// <summary>
        /// 添加一个段落
        /// </summary>
        /// <param name="text">段落里的文本</param>
        /// <returns></returns>
        public abstract CompositeElementBase AppendParagraph(string text);
        /// <summary>
        /// 添加一个段落
        /// </summary>
        /// <param name="text">段落里的文本</param>
        /// <param name="style">适用样式集</param>
        /// <returns></returns>
        public abstract CompositeElementBase AppendParagraph(string text, WordStyles style);
        /// <summary>
        /// 添加文字
        /// </summary>
        /// <param name="text">待添加的文字</param>
        /// <returns></returns>
        public abstract CompositeElementBase Append(string text);
        /// <summary>
        /// 添加文字
        /// </summary>
        /// <param name="text">待添加的文字</param>
        /// <param name="style">适用的样式集</param>
        /// <returns></returns>
        public abstract CompositeElementBase Append(string text, WordStyles style);
        /// <summary>
        /// 设置字体
        /// </summary>
        /// <param name="family">字体</param>
        public abstract CompositeElementBase SetFontFamily(string family);
        /// <summary>
        /// 设置字体
        /// </summary>
        /// <param name="zwFamily">中文字体</param>
        /// <param name="asciiFamily">西文字体</param>
        public abstract CompositeElementBase SetFontFamily(string zwFamily, string asciiFamily);
        /// <summary>
        /// 设置字号
        /// </summary>
        /// <param name="sizePoint">单位：点</param>
        public abstract CompositeElementBase SetFontSize(int sizePoint);
        /// <summary>
        /// 设置字号
        /// </summary>
        /// <param name="size">单位：字号</param>
        public abstract CompositeElementBase SetFontSize(FontSize size);
        /// <summary>
        /// 设置粗体
        /// </summary>
        /// <param name="isBold">设置为false为取消</param>
        public abstract CompositeElementBase SetBold(bool isBold = true);
        /// <summary>
        /// 设置斜体
        /// </summary>
        /// <param name="isItalic">设置为false为取消</param>
        public abstract CompositeElementBase SetItalic(bool isItalic = true);
        /// <summary>
        /// 设置下划线
        /// </summary>
        /// <param name="isUnderline">设置为false为取消</param>
        /// <param name="lineStyle">设置下划线样式</param>
        public abstract CompositeElementBase SetUnderline(UnderlineStyle lineStyle = UnderlineStyle.Single);
        /// <summary>
        /// 设置文字对齐
        /// </summary>
        /// <param name="align">对齐方式，默认左对齐</param>
        public abstract CompositeElementBase SetAlign(TextAlign align = TextAlign.Left);
        /// <summary>
        /// 设置首行缩进
        /// </summary>
        /// <param name="chars">字符数</param>
        public abstract CompositeElementBase SetFirstLineIndent(double chars = 2);
        /// <summary>
        /// 设置左侧缩进字符
        /// </summary>
        /// <param name="chars"></param>
        /// <returns></returns>
        public abstract CompositeElementBase SetLeftChars(double chars = 0);
        /// <summary>
        /// 设置右侧缩进字符
        /// </summary>
        /// <param name="chars"></param>
        /// <returns></returns>
        public abstract CompositeElementBase SetRightChars(double chars = 0);
        /// <summary>
        /// 设置行高
        /// </summary>
        /// <param name="times">单位：倍数</param>
        public abstract CompositeElementBase SetLineSpacing(double times);
        /// <summary>
        /// 设置段前间距
        /// </summary>
        /// <param name="pt">单位：磅</param>
        public abstract CompositeElementBase SpaceingBefore(int pt);
        /// <summary>
        /// 设置段后间距
        /// </summary>
        /// <param name="pt">单位：磅</param>
        public abstract CompositeElementBase SpacingAfter(int pt);
        #endregion

        #region 流式接口 - 设置样式
        public CompositeElementBase SetStyle(WordStyles style)
        {
            if (style == null)
            {
                return this;
            }

            return SetStyleSafe(style);
        }
        protected abstract CompositeElementBase SetStyleSafe(WordStyles style);
        #endregion
    }
}
